<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>k8s部署 | Lyra</title><meta name="author" content="lyra"><meta name="copyright" content="lyra"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="0.常用命令k8s官方文档12# 常用https:&#x2F;&#x2F;kubernetes.io&#x2F;zh-cn&#x2F;docs&#x2F;reference&#x2F;kubernetes-api&#x2F;   # api文档  kubeadm常用工具功能1234567kubeadm init：初始化一个Master节点kubeadm join：将工作节点加入集群kubeadm upgrade：升级K8s版本kubeadm token：管理 kub">
<meta property="og:type" content="article">
<meta property="og:title" content="k8s部署">
<meta property="og:url" content="https://gitee.com/Lyra3/blog/2023/08/14/k8s%E9%83%A8%E7%BD%B2/index.html">
<meta property="og:site_name" content="Lyra">
<meta property="og:description" content="0.常用命令k8s官方文档12# 常用https:&#x2F;&#x2F;kubernetes.io&#x2F;zh-cn&#x2F;docs&#x2F;reference&#x2F;kubernetes-api&#x2F;   # api文档  kubeadm常用工具功能1234567kubeadm init：初始化一个Master节点kubeadm join：将工作节点加入集群kubeadm upgrade：升级K8s版本kubeadm token：管理 kub">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/Lyra3/blog/img/avatar.png">
<meta property="article:published_time" content="2023-08-14T12:27:33.377Z">
<meta property="article:modified_time" content="2023-08-14T15:48:52.623Z">
<meta property="article:author" content="lyra">
<meta property="article:tag" content="应用部署">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/Lyra3/blog/img/avatar.png"><link rel="shortcut icon" href="/blog/img/favicon1.png"><link rel="canonical" href="https://gitee.com/Lyra3/blog/2023/08/14/k8s%E9%83%A8%E7%BD%B2/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'k8s部署',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-14 23:48:52'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/blog/atom.xml" title="Lyra" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">Articles</div><div class="length-num">4</div></a><a href="/blog/tags/"><div class="headline">Tags</div><div class="length-num">2</div></a><a href="/blog/categories/"><div class="headline">Categories</div><div class="length-num">4</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/blog/" title="Lyra"><span class="site-name">Lyra</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">k8s部署</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-08-14T12:27:33.377Z" title="Created 2023-08-14 20:27:33">2023-08-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-08-14T15:48:52.623Z" title="Updated 2023-08-14 23:48:52">2023-08-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/k8s/">k8s</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="k8s部署"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="0-常用命令"><a href="#0-常用命令" class="headerlink" title="0.常用命令"></a>0.常用命令</h2><h3 id="k8s官方文档"><a href="#k8s官方文档" class="headerlink" title="k8s官方文档"></a>k8s官方文档</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 常用</span></span><br><span class="line">https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/   <span class="comment"># api文档</span></span><br></pre></td></tr></table></figure>

<h3 id="kubeadm常用工具功能"><a href="#kubeadm常用工具功能" class="headerlink" title="kubeadm常用工具功能"></a>kubeadm常用工具功能</h3><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">kubeadm</span> init：初始化一个Master节点</span><br><span class="line"><span class="attribute">kubeadm</span> join：将工作节点加入集群</span><br><span class="line"><span class="attribute">kubeadm</span> upgrade：升级K8s版本</span><br><span class="line"><span class="attribute">kubeadm</span> token：管理 kubeadm join 使用的令牌</span><br><span class="line"><span class="attribute">kubeadm</span> reset：清空 kubeadm init 或者 kubeadm join 对主机所做的任何更改</span><br><span class="line"><span class="attribute">kubeadm</span> version：打印 kubeadm 版本</span><br><span class="line"><span class="attribute">kubeadm</span> alpha：预览可用的新功能</span><br></pre></td></tr></table></figure>

<h3 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看所有节点pod的运行状态</span></span><br><span class="line">kubectl get pod,svc -o wide</span><br><span class="line">kubectl get pods --all-namespaces -o wide</span><br><span class="line">kubectl get pods -n kubernetes-dashboard  -o wide</span><br><span class="line"><span class="comment"># 通过watch监控</span></span><br><span class="line">watch -n 1 kubectl get pods -A -o wide</span><br><span class="line"><span class="comment"># 普通查看</span></span><br><span class="line">kubectl get pods      -n kubernetes-dashboard</span><br><span class="line"><span class="comment"># 带节点查看查看</span></span><br><span class="line">kubectl get pods,svc  -n kubernetes-dashboard</span><br><span class="line"><span class="comment"># 详细查看</span></span><br><span class="line">kubectl describe pods -n kubernetes-dashboard kubernetes-dashboard-576cb95f94-qgfh6 </span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过命令行工具kubectl 获取异常容器</span></span><br><span class="line">kubectl get pods -n kubernetes-dashboard | grep -v Running</span><br><span class="line"><span class="comment"># 查看日志</span></span><br><span class="line">kubectl logs kubernetes-dashboard-576cb95f94-qgfh6 -n kubernetes-dashboard</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建/删除命名空间</span></span><br><span class="line">kubectl create ns app-dev</span><br><span class="line">kubectl delete ns app-dev</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建pod</span></span><br><span class="line"><span class="comment"># 不存在则创建，存在则报错</span></span><br><span class="line">kubectl create -f docker-appmanager.yaml</span><br><span class="line"><span class="comment"># 不存在则创建，存在则更新</span></span><br><span class="line">kubectl apply  -f docker-appmanager.yaml</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除pod</span></span><br><span class="line">kubectl delete pod jenkins2-8698b5449c-grbdm -n jenkins</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看deployment</span></span><br><span class="line">kubectl get deployment -n app-dev</span><br><span class="line">kubectl get deploy my-dep -o yaml <span class="comment"># 通过yaml方式查看</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除deployment(删除pod)</span></span><br><span class="line">kubectl delete deployment appmanager -n app-dev</span><br><span class="line">kubectl delete deployment nginx  -n default</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看k8s网段</span></span><br><span class="line">kubectl get configmap kubeadm-config -n kube-system -o yaml</span><br><span class="line"></span><br><span class="line"><span class="comment"># 滚动更新</span></span><br></pre></td></tr></table></figure>

<h3 id="重新初始化k8s"><a href="#重新初始化k8s" class="headerlink" title="重新初始化k8s"></a>重新初始化k8s</h3><p>1、移除所有工作节点</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl delete node node1</span><br></pre></td></tr></table></figure>

<p>2、所有【工作节点】删除工作目录，并重置kubeadm</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">rm</span> -rf /etc/kubernetes/*</span><br><span class="line">kubeadm reset</span><br></pre></td></tr></table></figure>

<p>3、【Master节点】删除工作目录，并重置kubeadm</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">rm</span> -rf /etc/kubernetes/*</span><br><span class="line"><span class="built_in">rm</span> -rf ~/.kube/*</span><br><span class="line"><span class="built_in">rm</span> -rf /var/lib/etcd/*</span><br><span class="line">kubeadm reset -f</span><br></pre></td></tr></table></figure>

<p>4、重新init kubernetes</p>
<h2 id="1-主机初始化"><a href="#1-主机初始化" class="headerlink" title="1.主机初始化"></a>1.主机初始化</h2><blockquote>
<p>所有k8s集群的主机都要完成以下操作</p>
</blockquote>
<p>根据规划设置主机名【主机名不能用下划线】</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hostnamectl set-hostname k8s-master</span><br></pre></td></tr></table></figure>

<p>对所有主机进行操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 关闭selinux</span></span><br><span class="line">setenforce 0  <span class="comment"># 临时</span></span><br><span class="line">sed -i <span class="string">&#x27;s/enforcing/disabled/&#x27;</span> /etc/selinux/config  <span class="comment"># 永久</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 关闭swap, 否则报错</span></span><br><span class="line">swapoff -a  <span class="comment"># 临时</span></span><br><span class="line">sed -ri <span class="string">&#x27;s/.*swap.*/#&amp;/&#x27;</span> /etc/fstab    <span class="comment"># 永久</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将桥接的IPv4流量传递到iptables的链</span></span><br><span class="line"><span class="built_in">cat</span> &gt; /etc/sysctl.d/k8s.conf &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">net.bridge.bridge-nf-call-ip6tables = 1</span></span><br><span class="line"><span class="string">net.bridge.bridge-nf-call-iptables = 1</span></span><br><span class="line"><span class="string">EOF</span></span><br><span class="line">sysctl --system  <span class="comment"># 生效</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 时间同步</span></span><br><span class="line">yum install ntpdate -y</span><br><span class="line">ntpdate time.windows.com</span><br></pre></td></tr></table></figure>

<p>添加hosts</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> &gt;&gt; /etc/hosts &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">10.192.2.205    cluster-endpoint        k8s-master</span></span><br><span class="line"><span class="string">10.192.2.206    k8s-node1       k8s-node1</span></span><br><span class="line"><span class="string">10.192.2.207    k8s-node2       k8s-node2</span></span><br><span class="line"><span class="string">EOF</span></span><br></pre></td></tr></table></figure>

<h3 id="安装docker-更改cgroup"><a href="#安装docker-更改cgroup" class="headerlink" title="安装docker,更改cgroup"></a>安装docker,更改cgroup</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置yum源</span></span><br><span class="line">sudo yum install -y yum-utils</span><br><span class="line"></span><br><span class="line">sudo yum-config-manager \</span><br><span class="line">--add-repo \</span><br><span class="line">http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装docker</span></span><br><span class="line">yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7  containerd.io-1.4.6</span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动docker</span></span><br><span class="line">systemctl <span class="built_in">enable</span> docker --now</span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置加速(镜像仓库和私有仓库)</span></span><br><span class="line">sudo <span class="built_in">mkdir</span> -p /etc/docker</span><br><span class="line"><span class="built_in">cat</span> &gt; /etc/docker/daemon.json &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">  &quot;exec-opts&quot;: [&quot;native.cgroupdriver=systemd&quot;],</span></span><br><span class="line"><span class="string">  &quot;registry-mirrors&quot;: [&quot;https://o5m1etng.mirror.aliyuncs.com&quot;],</span></span><br><span class="line"><span class="string">  &quot;insecure-registries&quot;: [&quot;https://core.harbor.devops.cloud.chinacdc.com&quot;]</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">EOF</span></span><br><span class="line"><span class="comment"># 重启docker</span></span><br><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>

<h3 id="安装k8s"><a href="#安装k8s" class="headerlink" title="安装k8s"></a>安装k8s</h3><p>配置阿里云YUM源</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> &gt; /etc/yum.repos.d/kubernetes.repo &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">[kubernetes]</span></span><br><span class="line"><span class="string">name=Kubernetes</span></span><br><span class="line"><span class="string">baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64</span></span><br><span class="line"><span class="string">enabled=1</span></span><br><span class="line"><span class="string">gpgcheck=0</span></span><br><span class="line"><span class="string">repo_gpgcheck=0</span></span><br><span class="line"><span class="string">gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg</span></span><br><span class="line"><span class="string">EOF</span></span><br></pre></td></tr></table></figure>

<p>安装kubeadm，kubelet，kubectl</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看K8S版本</span></span><br><span class="line">yum list kubelet --showduplicates | <span class="built_in">sort</span> -r</span><br><span class="line"><span class="comment"># 安装指定版本的kubelet</span></span><br><span class="line">yum install -y kubelet-1.21.14 kubeadm-1.21.14 kubectl-1.21.14</span><br><span class="line"> <span class="comment"># 设置开机启动kubelet</span></span><br><span class="line">systemctl <span class="built_in">enable</span> --now kubelet</span><br></pre></td></tr></table></figure>

<h2 id="2-master节点初始化"><a href="#2-master节点初始化" class="headerlink" title="2.master节点初始化"></a>2.master节点初始化</h2><blockquote>
<p>该过程在master节点进行操作</p>
</blockquote>
<h3 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h3><h4 id="开放端口"><a href="#开放端口" class="headerlink" title="开放端口"></a>开放端口</h4><blockquote>
<p>如果是云主机要开放对k8s集群内的主机设置相应的安全组</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --zone=public --add-port=6443/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=8080/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=2379-2380/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=10250-10255/tcp --permanent</span><br><span class="line"><span class="comment"># kubernetes-dashboard</span></span><br><span class="line">firewall-cmd --zone=public --add-port=179/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=443/tcp --permanent</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<h4 id="执行kubeadm-init"><a href="#执行kubeadm-init" class="headerlink" title="执行kubeadm init"></a>执行kubeadm init</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">kubeadm init \</span><br><span class="line">  --apiserver-advertise-address=10.192.2.205 \</span><br><span class="line">  --image-repository registry.aliyuncs.com/google_containers \</span><br><span class="line">  --control-plane-endpoint=cluster-endpoint \</span><br><span class="line">  --kubernetes-version v1.21.14 \</span><br><span class="line">  --service-cidr=10.254.0.0/17 \</span><br><span class="line">  --pod-network-cidr=10.254.128.0/17 \</span><br><span class="line">  --ignore-preflight-errors=all</span><br><span class="line">  </span><br><span class="line"><span class="comment"># --apiserver-advertise-address 集群通告地址【master地址】</span></span><br><span class="line"><span class="comment"># --image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问，这里指定阿里云镜像仓库地址</span></span><br><span class="line"><span class="comment"># --kubernetes-version K8s版本，与上面安装的一致</span></span><br><span class="line"><span class="comment"># --service-cidr 集群内部虚拟网络，Pod统一访问入口</span></span><br><span class="line"><span class="comment"># --pod-network-cidr Pod网络，与下面部署的CNI网络组件yaml中保持一致【待会要用, 注意不要和服务器内网ip段冲突】</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 提示:</span></span><br><span class="line">如果有计划升级 单control-plane节点集群到 HA ，建议配置 --control-plane-endpoint 来设置 shared endpoint 来向所有 control-plane 节点提供访问。Endpoint 可以是 DNS name 或 负载均衡IP 。 </span><br><span class="line">【温馨提示】kubeadm 不支持将没有 --control-plane-endpoint 参数的单个控制平面集群转换为高可用性集群。</span><br></pre></td></tr></table></figure>

<p>拷贝kubectl使用的连接k8s认证文件到默认路径</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p <span class="variable">$HOME</span>/.kube</span><br><span class="line">sudo <span class="built_in">cp</span> -i /etc/kubernetes/admin.conf <span class="variable">$HOME</span>/.kube/config</span><br><span class="line">sudo <span class="built_in">chown</span> $(<span class="built_in">id</span> -u):$(<span class="built_in">id</span> -g) <span class="variable">$HOME</span>/.kube/config</span><br></pre></td></tr></table></figure>

<h4 id="备份加入节点的的指令"><a href="#备份加入节点的的指令" class="headerlink" title="备份加入节点的的指令"></a><strong>备份</strong>加入节点的的指令</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># node节点加入集群的指令</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#加入master节点</span></span><br><span class="line">kubeadm <span class="built_in">join</span> cluster-endpoint:6443 --token 8co3na.imgy8n1aoya7mp2n \</span><br><span class="line">        --discovery-token-ca-cert-hash sha256:1f0de7d81488c57178a64355c38962818baf079591a555e67dd08047d9834269 \</span><br><span class="line">        --control-plane</span><br><span class="line"></span><br><span class="line">Then you can <span class="built_in">join</span> any number of worker nodes by running the following on each as root:</span><br><span class="line"><span class="comment">#加入worker节点</span></span><br><span class="line">kubeadm <span class="built_in">join</span> cluster-endpoint:6443 --token 8co3na.imgy8n1aoya7mp2n \</span><br><span class="line">        --discovery-token-ca-cert-hash sha256:1f0de7d81488c57178a64355c38962818baf079591a555e67dd08047d9834269</span><br></pre></td></tr></table></figure>

<p>查看工作节点：</p>
<blockquote>
<p>此时STATUS为NotRead是正常状态</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">kubectl get nodes</span><br><span class="line">NAME STATUS ROLES AGE VERSION</span><br><span class="line">k8s-master1 NotReady control-plane,master 25s v1.21.2</span><br></pre></td></tr></table></figure>

<p>查看pods:</p>
<blockquote>
<p><code>coredns</code> 停滞在 <code>Pending</code> 状态</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kube-system   coredns-59d64cd4d4-58b42                   0/1     Pending  1          22s</span><br><span class="line">kube-system   coredns-59d64cd4d4-mtm4n                   0/1     Pending  1          22s</span><br></pre></td></tr></table></figure>

<p>这一行为是 <strong>预期之中</strong> 的，因为系统就是这么设计的。kubeadm 的网络供应商是中立的， 因此管理员应该选择<a target="_blank" rel="noopener" href="https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/addons/">安装 Pod 的网络插件</a>。 你必须完成 Pod 的网络配置，然后才能完全部署 CoreDNS。 在网络被配置好之前，DNS 组件会一直处于 <code>Pending</code> 状态。</p>
</blockquote>
<h3 id="master部署容器网络（CNI）"><a href="#master部署容器网络（CNI）" class="headerlink" title="master部署容器网络（CNI）"></a>master部署容器网络（CNI）</h3><p>Calico（CNI）是一个纯三层的数据中心网络方案，是目前Kubernetes主流的网络方案</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml</span><br></pre></td></tr></table></figure>

<p>1）下载修改Pod网络配置项（<strong>CALICO_IPV4POOL_CIDR</strong>），需要和前面kubeadm init的 –pod-network-cidr里的一样</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">- name: CALICO_IPV4POOL_CIDR</span><br><span class="line">  value: <span class="string">&quot;10.254.128.0/17&quot;</span></span><br></pre></td></tr></table></figure>

<p>2）在CLUSTER_TYPE后添加信息， 否则之后创建镜像时可能会出现<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_45053774/article/details/130720712">Failed to create pod sandbox: rpc error: code &#x3D; Unknown desc &#x3D; [failed to set up sandbox container…</a></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">CLUSTER_TYPE</span></span><br><span class="line">  <span class="attr">value:</span> <span class="string">&quot;k8s,bgp&quot;</span></span><br><span class="line">  <span class="comment"># 在CLUSTER_TYPE后面添加 (eth0为机器对应的网卡名称)</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">IP_AUTODETECTION_METHOD</span></span><br><span class="line">  <span class="attr">value:</span> <span class="string">&quot;interface=eth0&quot;</span></span><br></pre></td></tr></table></figure>

<p>3）修改完进行部署</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl apply -f calico.yaml</span><br><span class="line">kubectl get pods -n kube-system</span><br></pre></td></tr></table></figure>

<h2 id="3-node节点加入集群"><a href="#3-node节点加入集群" class="headerlink" title="3.node节点加入集群"></a>3.node节点加入集群</h2><blockquote>
<p>该过程在node节点进行操作</p>
<p>确保node节点已经进行了主机初始化操作</p>
</blockquote>
<h3 id="开放端口-1"><a href="#开放端口-1" class="headerlink" title="开放端口"></a>开放端口</h3><blockquote>
<p>如果是云主机要开放对k8s集群内的主机设置相应的安全组</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># kubernetes-dashboard</span></span><br><span class="line">firewall-cmd --zone=public --add-port=6443/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=30000-32767/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=10250-10259/tcp --permanent</span><br><span class="line"><span class="comment"># kubernetes-dashboard</span></span><br><span class="line">firewall-cmd --zone=public --add-port=179/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=443/tcp --permanent</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<p>在 <code>node</code>节点中使用上面加入集群的指令将node节点加入到k8s集群</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># node节点加入集群的指令</span></span><br><span class="line">kubeadm <span class="built_in">join</span> cluster-endpoint:6443 --token 8co3na.imgy8n1aoya7mp2n \</span><br><span class="line">        --discovery-token-ca-cert-hash sha256:1f0de7d81488c57178a64355c38962818baf079591a555e67dd08047d9834269</span><br></pre></td></tr></table></figure>

<h2 id="4-master节点安装dashboard"><a href="#4-master节点安装dashboard" class="headerlink" title="4.master节点安装dashboard"></a>4.master节点安装dashboard</h2><h3 id="安装nginx验证k8s集群"><a href="#安装nginx验证k8s集群" class="headerlink" title="安装nginx验证k8s集群"></a>安装nginx验证k8s集群</h3><p>添加一个nginx看看是否正常运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">kubectl create deployment nginx --image=nginx</span><br><span class="line">kubectl expose deployment nginx --port=80 --<span class="built_in">type</span>=NodePort</span><br><span class="line">kubectl get pod,svc -o wide</span><br></pre></td></tr></table></figure>

<h3 id="安装dashboard"><a href="#安装dashboard" class="headerlink" title="安装dashboard"></a>安装dashboard</h3><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/kubernetes/dashboard/releases/tag/v2.4.0">dashboard</a>  是官方提供的一个UI，可以基本管理K8s资源。<br>需要注意每个版本对应的k8s版本都不同，<a target="_blank" rel="noopener" href="https://www.cnblogs.com/gys001/p/16881393.html">安装</a> 前需要先确认</p>
</blockquote>
<p>下载yaml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml</span><br></pre></td></tr></table></figure>

<p>修改kubernetes-dashboard.yaml(<code>&lt;font color=&quot;[#00BFFF](https://www.w3school.com.cn/tiy/color.asp?hex=00BFFF)&quot;&gt;</code>使用具体下载过来的文件名 <code>&lt;/font&gt;</code>),将dashboard部署到master节点</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 修改kubernetes-dashboard的Service类型</span></span><br><span class="line">kind: Service</span><br><span class="line">apiVersion: v1</span><br><span class="line">metadata:</span><br><span class="line">  labels:</span><br><span class="line">    k8s-app: kubernetes-dashboard</span><br><span class="line">  name: kubernetes-dashboard</span><br><span class="line">  namespace: kubernetes-dashboard</span><br><span class="line">spec:</span><br><span class="line">  <span class="built_in">type</span>: NodePort    		  <span class="comment"># 新增</span></span><br><span class="line">  ports:</span><br><span class="line">    - port: 443</span><br><span class="line">      targetPort: 8443</span><br><span class="line">      nodePort: 30010		  <span class="comment"># 新增 # 注意不指定将随机生成端口</span></span><br><span class="line">  selector:</span><br><span class="line">    k8s-app: kubernetes-dashboard</span><br></pre></td></tr></table></figure>

<p>部署</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 部署</span></span><br><span class="line">kubectl create -f recommended.yaml</span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">kubectl get pod,svc -n kubernetes-dashboard</span><br></pre></td></tr></table></figure>

<p>创建service account并绑定默认cluster-admin管理员集群角色：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建用户</span></span><br><span class="line">kubectl create serviceaccount dashboard-admin -n kube-system</span><br><span class="line"><span class="comment"># 用户授权</span></span><br><span class="line">kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin</span><br><span class="line"><span class="comment"># 获取用户Token</span></span><br><span class="line">kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk <span class="string">&#x27;/dashboard-admin/&#123;print $1&#125;&#x27;</span>)</span><br><span class="line">-----------------------------Token--------------------------------</span><br><span class="line">Name:         dashboard-admin-token-fz8v4</span><br><span class="line">Namespace:    kube-system</span><br><span class="line">Labels:       &lt;none&gt;</span><br><span class="line">Annotations:  kubernetes.io/service-account.name: dashboard-admin</span><br><span class="line">              kubernetes.io/service-account.uid: 650d64a9-42a4-4152-ad6b-6567acc4b290</span><br><span class="line"></span><br><span class="line">Type:  kubernetes.io/service-account-token</span><br><span class="line"></span><br><span class="line">Data</span><br><span class="line">====</span><br><span class="line">ca.crt:     1066 bytes</span><br><span class="line">namespace:  11 bytes</span><br><span class="line">token:  	<span class="comment">#复制以下信息到浏览器输入</span></span><br><span class="line">eyJhbGciOiJSUzI1NiIsImtpZCI6InNNd195WDhGekpYTjZ5akZBTDQ1OFJrUGMzc1RlSU9nNjZPaGhWN1h1TGMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tZno4djQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNjUwZDY0YTktNDJhNC00MTUyLWFkNmItNjU2N2FjYzRiMjkwIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.Yo1Cd2sVbjv5e4ucOAz2tP8awSPN4CzrHq_N6F4kc4KtDlqzQRobIejhSk_oGxG8KMbFJwXmmRtfqy8_wJR1ARlv1vXzvCnOYv5UsD6xvJWT8XQ7SZ9NcOwqm1przJ_oUXhzsOUCpdppHx52G3CKSNkPhRDbUpqmPdZlQyVse52Sn_Y-aWW2nfBiV8TSJmr8zPKrY4-xXweuQ070PKrzY74jZ97AQikcFRxadvWA9JWdBqnv_vJP0smOESG9H5AuzBYlZFZdpdNFigaZbRzpCS7UnsmHXXlWrGoH5pKuOchI7f74pB7PrfsHHEODjHWKozVW4L6HZXUXRMvknqmssw</span><br><span class="line">-----------------------------Token--------------------------------</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>通过浏览器进入dashboard</p>
<p><a target="_blank" rel="noopener" href="https://10.192.2.205:30010/">https://10.192.2.205:30010/</a></p>
<p>使用输出的token登录dashboard</p>
<ul>
<li>页面显示您的连接不是私密链接: 在空白处输入 <code>&lt;font color=&quot;#aa0000&quot;&gt;</code>thisisunsafe <code>&lt;/font&gt;</code>并回车</li>
</ul>
<h3 id="重装dashboard"><a href="#重装dashboard" class="headerlink" title="重装dashboard"></a>重装dashboard</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># kubectl get deployment -A 找到对应的deployment NAME</span></span><br><span class="line">kubectl delete deployment kubernetes-dashboard -n kubernetes-dashboard</span><br><span class="line"></span><br><span class="line"><span class="comment"># kubectl get service -A 找到对应的service NAME</span></span><br><span class="line">kubectl delete service kubernetes-dashboard  -n kubernetes-dashboard</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重新部署</span></span><br><span class="line">kubectl apply -f dashboard.yaml</span><br></pre></td></tr></table></figure>

<h3 id="部署dashboard常见错误"><a href="#部署dashboard常见错误" class="headerlink" title="部署dashboard常见错误"></a>部署dashboard常见错误</h3><h4 id="1-kubernetes-dashboard后出现CrashLoopBackOff"><a href="#1-kubernetes-dashboard后出现CrashLoopBackOff" class="headerlink" title="1.kubernetes-dashboard后出现CrashLoopBackOff"></a>1.kubernetes-dashboard后出现CrashLoopBackOff</h4><blockquote>
<p>node节点部署kubernetes-dashboard后出现CrashLoopBackOff,有可能是防火墙设置了策略限制导致,在node节点中将防火墙关闭或设置放行规则即可</p>
<p>在修改防火墙后需要重启docker</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">kubernetes-dashboard   kubernetes-dashboard-576cb95f94-9dd8l       0/1     CrashLoopBackOff   0          47s   10.244.166.143    node1    &lt;none&gt;           &lt;none&gt;</span></span><br></pre></td></tr></table></figure>

<p>解决:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">------------------------------------------------------------------------</span><br><span class="line">systemctl stop firewalld </span><br><span class="line"><span class="comment"># or</span></span><br><span class="line">firewall-cmd --zone=public --add-port=30000-32767/tcp --permanent <span class="comment">#云平台中则是开放安全组</span></span><br><span class="line">firewall-cmd --reload</span><br><span class="line">------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改网络后需要重启docker</span></span><br><span class="line">systemctl restart docker</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重新部署dashboard</span></span><br><span class="line">kubectl apply -f dashboard.yaml</span><br></pre></td></tr></table></figure>

<h4 id="2-配置kubernetes-dashboard后无法通过任意ip访问"><a href="#2-配置kubernetes-dashboard后无法通过任意ip访问" class="headerlink" title="2.配置kubernetes-dashboard后无法通过任意ip访问"></a>2.配置kubernetes-dashboard后无法通过任意ip访问</h4><blockquote>
<p>k8s配置了–control-plane-endpoint但是部署服务:如dashboard，无法通过任意ip访问，有可能是防火墙设置了策略限制导致，在需要访问相应服务的节点中将防火墙关闭或设置放行规则即可</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">------------------------------------------------------------------------</span><br><span class="line">systemctl stop firewalld </span><br><span class="line"><span class="comment"># or</span></span><br><span class="line">firewall-cmd --zone=public --add-port=30000-32767/tcp --permanent <span class="comment">#云平台中则是开放安全组</span></span><br><span class="line">firewall-cmd --reload</span><br><span class="line">------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改网络后需要重启docker</span></span><br><span class="line">systemctl restart docker</span><br></pre></td></tr></table></figure>

<h2 id="5-拉取Harbor镜像"><a href="#5-拉取Harbor镜像" class="headerlink" title="5.拉取Harbor镜像"></a>5.拉取Harbor镜像</h2><h3 id="yaml"><a href="#yaml" class="headerlink" title="yaml"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/syushin/p/14768872.html">yaml</a></h3><p>配置远程harbor仓库后, 对生成的秘钥文件进行base64加密</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> ~/.docker/config.json |<span class="built_in">base64</span> -w 0</span><br><span class="line">ewoJImF1dGhzIjogewoJCSJjb3JlLmhhcmJvci5kZXZvcHMuY2xvdWQuY2hpbmFjZGMuY29tIjogewoJCQkiYXV0aCI6ICJiSEYzZDNsdFlXbHNRREUyTXk1amIyMDZUSEYzZDNsdFlXbHNPVEEzTURZMyIKCQl9Cgl9Cn0=</span><br></pre></td></tr></table></figure>

<p>K8S创建secret秘钥</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">cat</span> <span class="string">&gt;</span> <span class="string">docker-secret.yaml</span> <span class="string">&lt;&lt;</span> <span class="string">EOF</span></span><br><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Secret</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">docker-login</span></span><br><span class="line">  <span class="attr">namespace:</span> <span class="string">app-dev</span>  <span class="comment"># 绑定命名空间, 否则默认绑定default</span></span><br><span class="line"><span class="attr">type:</span> <span class="string">kubernetes.io/dockerconfigjson</span></span><br><span class="line"><span class="attr">data:</span></span><br><span class="line">  <span class="string">.dockerconfigjson:</span> <span class="string">ewoJImF1dGhzIjogewoJCSJjb3JlLmhhcmJvci5kZXZvcHMuY2xvdWQuY2hpbmFjZGMuY29tIjogewoJCQkiYXV0aCI6ICJiSEYzZDNsdFlXbHNRREUyTXk1amIyMDZUSEYzZDNsdFlXbHNPVEEzTURZMyIKCQl9Cgl9Cn0=</span></span><br><span class="line"><span class="string">EOF</span></span><br></pre></td></tr></table></figure>

<p>创建secret</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create -f docker-secret.yaml</span><br></pre></td></tr></table></figure>

<p>查看secret</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get secret</span><br></pre></td></tr></table></figure>

<h3 id="单命令"><a href="#单命令" class="headerlink" title="单命令"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/wjhlinux/p/10267626.html">单命令</a></h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create secret docker-registry docker-login --docker-server=192.168.1.200 --docker-username=admin --docker-password=Harbor12345</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/wuzhenxu1995/article/details/130363177">Secret需要添加命名空间(-n app-dev), 否则会出现ImagePullBackOff</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create secret docker-registry harbor-app-dev --docker-server=192.168.1.200 --docker-username=admin --docker-password=Harbor12345 -n app-dev</span><br></pre></td></tr></table></figure>

<h2 id="附录"><a href="#附录" class="headerlink" title="附录"></a>附录</h2><h3 id="常见错误"><a href="#常见错误" class="headerlink" title="常见错误"></a>常见错误</h3><h4 id="1-pod内部无法通过域名访问service"><a href="#1-pod内部无法通过域名访问service" class="headerlink" title="1.pod内部无法通过域名访问service"></a>1.pod内部无法通过域名访问service</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.通过命令查看dns日志</span></span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> $(kubectl get pods --namespace=kube-system -l k8s-app=kube-dns -o name);   <span class="keyword">do</span> kubectl logs --namespace=kube-system <span class="variable">$p</span>; <span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 出现以下错误(没有dns能够解析)</span></span><br><span class="line">[ERROR] plugin/errors: 2 7626482442856297775.6253373384747610250. HINFO: <span class="built_in">read</span> udp 10.244.219.80:38070-&gt;8.8.8.8:53: i/o <span class="built_in">timeout</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://gitee.com/Lyra3/blog">lyra</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://gitee.com/Lyra3/blog/2023/08/14/k8s%E9%83%A8%E7%BD%B2/">https://gitee.com/Lyra3/blog/2023/08/14/k8s%E9%83%A8%E7%BD%B2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2/">应用部署</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/avatar.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-full"><a href="/blog/2023/08/14/mysql%E9%83%A8%E7%BD%B2/" title="mysql部署"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">mysql部署</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/blog/2023/08/15/kubebuilder%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" title="kubebuilder环境搭建"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-08-15</div><div class="title">kubebuilder环境搭建</div></div></a></div><div><a href="/blog/2023/08/14/mysql%E9%83%A8%E7%BD%B2/" title="mysql部署"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-08-14</div><div class="title">mysql部署</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">lyra</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">Articles</div><div class="length-num">4</div></a><a href="/blog/tags/"><div class="headline">Tags</div><div class="length-num">2</div></a><a href="/blog/categories/"><div class="headline">Categories</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wwwviolet"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/wwwviolet" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:lqwwymail@163.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">share</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#0-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-text">0.常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#k8s%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3"><span class="toc-text">k8s官方文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#kubeadm%E5%B8%B8%E7%94%A8%E5%B7%A5%E5%85%B7%E5%8A%9F%E8%83%BD"><span class="toc-text">kubeadm常用工具功能</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-text">常用命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E5%88%9D%E5%A7%8B%E5%8C%96k8s"><span class="toc-text">重新初始化k8s</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E4%B8%BB%E6%9C%BA%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">1.主机初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85docker-%E6%9B%B4%E6%94%B9cgroup"><span class="toc-text">安装docker,更改cgroup</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85k8s"><span class="toc-text">安装k8s</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-master%E8%8A%82%E7%82%B9%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">2.master节点初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%80%E6%94%BE%E7%AB%AF%E5%8F%A3"><span class="toc-text">开放端口</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%A7%E8%A1%8Ckubeadm-init"><span class="toc-text">执行kubeadm init</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%87%E4%BB%BD%E5%8A%A0%E5%85%A5%E8%8A%82%E7%82%B9%E7%9A%84%E7%9A%84%E6%8C%87%E4%BB%A4"><span class="toc-text">备份加入节点的的指令</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#master%E9%83%A8%E7%BD%B2%E5%AE%B9%E5%99%A8%E7%BD%91%E7%BB%9C%EF%BC%88CNI%EF%BC%89"><span class="toc-text">master部署容器网络（CNI）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-node%E8%8A%82%E7%82%B9%E5%8A%A0%E5%85%A5%E9%9B%86%E7%BE%A4"><span class="toc-text">3.node节点加入集群</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%80%E6%94%BE%E7%AB%AF%E5%8F%A3-1"><span class="toc-text">开放端口</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-master%E8%8A%82%E7%82%B9%E5%AE%89%E8%A3%85dashboard"><span class="toc-text">4.master节点安装dashboard</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85nginx%E9%AA%8C%E8%AF%81k8s%E9%9B%86%E7%BE%A4"><span class="toc-text">安装nginx验证k8s集群</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85dashboard"><span class="toc-text">安装dashboard</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E8%A3%85dashboard"><span class="toc-text">重装dashboard</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%83%A8%E7%BD%B2dashboard%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF"><span class="toc-text">部署dashboard常见错误</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-kubernetes-dashboard%E5%90%8E%E5%87%BA%E7%8E%B0CrashLoopBackOff"><span class="toc-text">1.kubernetes-dashboard后出现CrashLoopBackOff</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E9%85%8D%E7%BD%AEkubernetes-dashboard%E5%90%8E%E6%97%A0%E6%B3%95%E9%80%9A%E8%BF%87%E4%BB%BB%E6%84%8Fip%E8%AE%BF%E9%97%AE"><span class="toc-text">2.配置kubernetes-dashboard后无法通过任意ip访问</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E6%8B%89%E5%8F%96Harbor%E9%95%9C%E5%83%8F"><span class="toc-text">5.拉取Harbor镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#yaml"><span class="toc-text">yaml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%95%E5%91%BD%E4%BB%A4"><span class="toc-text">单命令</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%99%84%E5%BD%95"><span class="toc-text">附录</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF"><span class="toc-text">常见错误</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-pod%E5%86%85%E9%83%A8%E6%97%A0%E6%B3%95%E9%80%9A%E8%BF%87%E5%9F%9F%E5%90%8D%E8%AE%BF%E9%97%AEservice"><span class="toc-text">1.pod内部无法通过域名访问service</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2023/09/07/linux%E5%B8%B8%E7%94%A8/" title="linux 常用">linux 常用</a><time datetime="2023-09-06T22:42:27.000Z" title="Created 2023-09-07 06:42:27">2023-09-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2023/08/15/kubebuilder%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" title="kubebuilder环境搭建">kubebuilder环境搭建</a><time datetime="2023-08-15T14:16:06.526Z" title="Created 2023-08-15 22:16:06">2023-08-15</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2023/08/14/mysql%E9%83%A8%E7%BD%B2/" title="mysql部署">mysql部署</a><time datetime="2023-08-14T15:32:43.794Z" title="Created 2023-08-14 23:32:43">2023-08-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2023/08/14/k8s%E9%83%A8%E7%BD%B2/" title="k8s部署">k8s部署</a><time datetime="2023-08-14T12:27:33.377Z" title="Created 2023-08-14 20:27:33">2023-08-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By lyra</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>